<template>
    <div>
        <!-- 搜索组件 -->
        <search-form :items="formItems" :params="params" :on-submit="onSearch" :btn-loading="loading" />

        <!-- 表格区域 -->
        <div class="table-body">
            <!-- 左上角按钮 -->
            <div class="table-left-top">
                <el-radio-group v-model="level" @change="onLevelChange" size="medium">
                    <el-radio-button v-for="(item, index) in collectionLevelList" :label="item.label" :key="index"></el-radio-button>
                </el-radio-group>
            </div>

            <!-- 表格 -->
            <el-table border style="width: 100%;" :data="data" v-loading="loading">
                <el-table-column align="center" prop="name" :label="$i18n.t('NAME')" :min-width="150" fixed="left"></el-table-column>
                <el-table-column align="center" prop="case_num" :label="$i18n.t('CASE_NUM')" :min-width="100" fixed="left"></el-table-column>
                <el-table-column align="center" prop="complete_num" :label="$i18n.t('COMPLETE_SUCCESS_NUM')" :min-width="100"></el-table-column>
                <el-table-column align="center" prop="renewal_num" :label="$i18n.t('RENEWAL_SUCCESS_NUM')" :min-width="100"></el-table-column>
                <el-table-column align="center" prop="recovery_rate" :label="$i18n.t('RECOVERY_RATE')" :min-width="100"></el-table-column>
                <el-table-column align="center" prop="case_amount" :label="$i18n.t('ENTER_COLLECTION_AMOUNT')" :min-width="200"></el-table-column>
                <el-table-column align="center" prop="complete_amount" :label="$i18n.t('SETTLE_AMOUNT')" :min-width="200"></el-table-column>
                <el-table-column align="center" prop="renewal_amount" :label="$i18n.t('RENEWAL_AMOUNT')" :min-width="200"></el-table-column>
                <el-table-column align="center" prop="amount_recovery_rate" :label="$i18n.t('AMOUNT_RECOVERY_RATE')" :min-width="100"></el-table-column>
            </el-table>

            <!-- 分页 -->
            <div class="table-pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next, ->, sizes"
                    :page-size.sync="page.perPage"
                    :total="page.total"
                    :page-count="page.pageCount"
                    :current-page.sync="page.currentPage"
                    :page-sizes="PAGES_SIZE"
                    @current-change="onCurrentPageChange"
                    @size-change="onPerPageChange"
                >
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
    import SearchForm from '@/components/SearchForm'
    import { getAchievement } from '@/api/collection'
    import { PAGES_SIZE } from '@/config/constants'

    export default {
        name: 'FlatRecord',
        components: { SearchForm },
        data () {
            return {
                PAGES_SIZE,
                params: {},
                data: [],
                page: {
                    total: 0,
                    currentPage: 1,
                    perPage: PAGES_SIZE[0],
                    pageCount: 1
                },
                loading: false,
                level: this.$i18n.t('ALL')
            }
        },
        created () {
            this.fetch()
        },
        computed: {
            formItems () {
                // 构建搜索表单
                return [
                    { title: this.$i18n.t('CASE_CREATE_TIME'), type: 'date-range', key: 'case_create_time' }
                ]
            },
            collectionLevelList () {
                const level = [...this.$store.getters.options.CollectionLevelList]
                level.unshift({
                    label: this.$i18n.t('ALL'),
                    value: 'all'
                })
                return level
            }
        },
        methods: {
            // 搜索按钮
            onSearch () {
                this.fetch()
            },
            // 从接口拉取数据
            fetch () {
                this.loading = true
                getAchievement({ ...this.params, page: this.page.currentPage, perPage: this.page.perPage }).then((res) => {
                    // eslint-disable-next-line camelcase
                    const { data, last_page, total } = res.result
                    this.data = data
                    this.page.total = total
                    // eslint-disable-next-line camelcase
                    this.page.pageCount = last_page
                }).finally(() => {
                    this.loading = false
                })
            },
            // 页数切换
            onCurrentPageChange (page) {
                this.page.currentPage = page
                this.fetch()
            },
            // 每页显示数量修改
            onPerPageChange (perPage) {
                this.page.perPage = perPage
                this.fetch()
            },
            // 阶段筛选
            onLevelChange () {
                const find = this.collectionLevelList.find(item => item.label === this.level)
                this.params.level = this.level === this.$i18n.t('ALL') || typeof find === 'undefined' ? null : find.value
                this.fetch()
            }
        }
    }
</script>

<style lang="less" scoped>
</style>
